/*
 * King.java
 *
 * Created on 18. �erven 2007, 20:04
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.possoft.serializable.chess.figurky;

import cz.possoft.generic.utils.GenericUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;
import cz.possoft.serializable.chess.game.ChessPole;

/**
 *
 * @author mara
 */
public class King extends Figurka { 
	
	public static final long serialVersionUID = 1089458945;
    
    /**
     * Creates a new instance of King
     */
    public King(int x, int y, int color, ChessPole sachovnice) {
        setX(x);
        setY(y);
        setColor(color);
        setSachovnice(sachovnice);
        setMarked(false);
    }        

    public boolean muzeTahnout2(int xCil, int yCil) {
        if ((xCil < 0) || (xCil > 7) || (yCil < 0) || (yCil > 7)) {
            return false;
        }
        if (getSachovnice().getFigurka(xCil, yCil).getColor() != getColor()) {
            if ((getX() - xCil == 1) && (getY() - yCil == 1)) {
                return true;
            }
            if ((getX() - xCil == 1) && (getY() - yCil == 0)) {
                return true;
            }
            if ((getX() - xCil == 1) && (getY() - yCil == -1)) {
                return true;
            }
            if ((getX() - xCil == 0) && (getY() - yCil == -1)) {
                return true;
            }
            if ((getX() - xCil == 0) && (getY() - yCil == 1)) {
                return true;
            }
            if ((getX() - xCil == -1) && (getY() - yCil == -1)) {
                return true;
            }
            if ((getX() - xCil == -1) && (getY() - yCil == 0)) {
                return true;
            }
            if ((getX() - xCil == -1) && (getY() - yCil == 1)) {
                return true;
            }
        }
        if (moznaRosada(xCil, yCil)) return true;
        return false;
    }
    
    public String getFigurkaName() {
        return PropertiesManager.getInstance().getMessageForKey(Text.KRAL);
    }
    
    // vraci true pokud kral muze udelat rosadu na zadane pole
    private boolean moznaRosada(int xCil, int yCil) {
        // kontrola jednoduchych podminek na rosadu
        //System.out.println("ROSADA: BOD1. XCIL=" + xCil + " YCIL=" + yCil);
        if (yCil != getY()) return false;
        if ((xCil != 6) && (xCil != 2)) return false;
        //System.out.println("ROSADA: BOD2");
        int towerX;
        int[] pomPole = new int[3];
        if (xCil == 2) {
           towerX = 0;
           pomPole[0] = 3;
           pomPole[1] = 2;
           pomPole[2] = 1;
        }
        else {
           towerX = 7;           
           pomPole[0] = 5;
           pomPole[1] = 6;
           pomPole[2] = 6;
        }
        
        if (isOdehralTah()) return false;
        Figurka pomTower = getSachovnice().getFigurka(towerX, yCil);
        if (!(pomTower instanceof Tower)) return false;
        if (pomTower.isOdehralTah()) return false;        
        //System.out.println("ROSADA: BOD3");
        
        // false pokud ma kral prave sach
        if ((getColor() == GenericUtils.COLOR_BLACK) && (getSachovnice().isSach(GenericUtils.COLOR_BLACK, getSachovnice().getBlackKingLocation().getX(), getSachovnice().getBlackKingLocation().getY()))) return false;
        if ((getColor() == GenericUtils.COLOR_WHITE) && (getSachovnice().isSach(GenericUtils.COLOR_WHITE, getSachovnice().getWhiteKingLocation().getX(), getSachovnice().getWhiteKingLocation().getY()))) return false;
        
        //System.out.println("ROSADA: BOD4");
        // na mezipolich se kontroluje jestli jsou prazdna a jestli na nekterem hrozi sach
        for (int i=0; i<pomPole.length; i++) {
            int xSou = pomPole[i];
            Figurka fig = getSachovnice().getFigurka(xSou, yCil);
            if (!(fig instanceof Prazdna)) return false;
            if ((i != 2) && (!nebudeSach(xSou, yCil))) return false;
        }                
        return true;
    }
}
